package com.ikun.yummy.activitys.search;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;

import com.google.android.material.tabs.TabLayout;
import com.ikun.yummy.R;
import com.ikun.yummy.activitys.details.DetailActivity;
import com.ikun.yummy.adapter.CookListAdapter;
import com.ikun.yummy.bean.CookInfo;
import com.ikun.yummy.manager.CookOpManager;
import com.ikun.yummy.manager.LikeOpManager;
import com.ikun.yummy.manager.UserOpManager;

import java.util.ArrayList;
import java.util.List;
public class SearchActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private static String queryStr;
    private static int type=0;
    private static CookOpManager cookOpManager;
    private static LikeOpManager likeOpManager;
    private static UserOpManager userOpManager;
    private static CookListAdapter adp;
    private static List<CookInfo> cookList = new ArrayList<>();
    private ListView list;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
    }
    private void initView() {
        tabLayout=findViewById(R.id.search_tablayout);
        tabLayout.addTab(tabLayout.newTab().setText("全部"));
        tabLayout.addTab(tabLayout.newTab().setText("荤菜"));
        tabLayout.addTab(tabLayout.newTab().setText("素菜"));
        tabLayout.addTab(tabLayout.newTab().setText("汤粥"));
        tabLayout.addTab(tabLayout.newTab().setText("面点"));
        tabLayout.addTab(tabLayout.newTab().setText("主食"));
        tabLayout.addTab(tabLayout.newTab().setText("小吃"));
        tabLayout.addTab(tabLayout.newTab().setText("西餐"));
        tabLayout.addTab(tabLayout.newTab().setText("饮品"));
        tabLayout.addTab(tabLayout.newTab().setText("养生"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                loadList(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }
            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });
        cookOpManager=new CookOpManager(this);
        userOpManager=new UserOpManager(this);
        likeOpManager=new LikeOpManager(this);
        adp=new CookListAdapter(this, R.layout.item_cook_no_like, cookList,2,null);
        list=findViewById(R.id.search_listview);
        list.setAdapter(adp);
        adp.notifyDataSetChanged();
        list.setOnItemClickListener((parent, view, position, id) -> {//列表点击
            Intent intent=new Intent(this, DetailActivity.class);
            intent.putExtra("cookid", cookList.get(position).cookid);
            startActivity(intent);
        });
        search("",0);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_search, menu);
        MenuItem item = menu.findItem(R.id.search_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setQueryHint("菜谱");
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                search(query,type);
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                queryStr=newText;
                if (newText.equals("")){
                    search(newText,type);
                }
                return false;
            }
        });
        return true;
    }
    public static void search(String key,int type){
        if (key==null) {
            key="";
        }
        cookList.clear();
        List<CookInfo> list = cookOpManager.queryBlur(key, getClassifications(type));
        if (list!=null){
            cookList.addAll(list);
        }
        adp.notifyDataSetChanged();
    }
    private static void loadList(int position) {
        type=position;
        search(queryStr,position);
    }
    public static String getClassifications(int i){
        String str="荤菜";
        switch (i){
            case 0:
                str="全部";
                break;
            case 1:
                str="荤菜";
                break;
            case 2:
                str="素菜";
                break;
            case 3:
                str="汤粥";
                break;
            case 4:
                str="面点";
                break;
            case 5:
                str="主食";
                break;
            case 6:
                str="小吃";
                break;
            case 7:
                str="西餐";
                break;
            case 8:
                str="饮品";
                break;
            case 9:
                str="养生";
                break;
        }
        return str;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}





